//
//  UIViewController+BarButtonItemAction.m
//  FM_Project
//
//  Created by chenzm on 2019/8/7.
//  Copyright © 2019 DL. All rights reserved.
//

#import "UIViewController+BarButtonItemAction.h"
#import <objc/runtime.h>
@implementation UIViewController (BarButtonItemAction)

static char leftBarButtonItemKey;
- (UIBarButtonItem *)leftBarButtonItem{
    return objc_getAssociatedObject(self, &leftBarButtonItemKey);
}
- (void)setLeftBarButtonItem:(UIBarButtonItem *)leftBarButtonItem{
    
    objc_setAssociatedObject(self, &leftBarButtonItemKey, leftBarButtonItem, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}




static char rightBarButtonItemKey;

- (UIBarButtonItem *)rightBarButtonItem{
    return objc_getAssociatedObject(self, &rightBarButtonItemKey);
}

- (void)setRightBarButtonItem:(UIBarButtonItem *)rightBarButtonItem{
    
    objc_setAssociatedObject(self, &rightBarButtonItemKey, rightBarButtonItem, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
}


-(void)defLeftBarImage:(NSString*)image{
    if (!self.leftBarButtonItem) {
        self.leftBarButtonItem=   [UIBarButtonItem itemWithTarget:self
                                                           action:@selector(touchNavLeftBtn:)
                                                            image:[UIImage imageNamed:image]];
        self.navigationItem.leftBarButtonItem =self.leftBarButtonItem;
    }
    UIButton *leftBnt=(UIButton*)self.leftBarButtonItem.customView;
    [ leftBnt setImage:[[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]forState:UIControlStateNormal];
}

-(void)defLeftBarTitle:(NSString*)title{
    
    if (!self.leftBarButtonItem) {
        
        self.leftBarButtonItem=   [UIBarButtonItem itemWithTarget:self
                                                           action:@selector(touchNavLeftBtn:)
                                                            title:title];
        self.navigationItem.leftBarButtonItem =self.leftBarButtonItem;
    }
    UIButton *leftBnt=(UIButton*)self.leftBarButtonItem.customView;
    [leftBnt setTitle:title forState:UIControlStateNormal];
    [leftBnt setTitleColor:kRGBA(255, 255, 255, 1)  forState:UIControlStateNormal];
}

-(void)defLeftBarTitle:(NSString*)title color:(UIColor *)titleColor{
    
    if (!self.leftBarButtonItem) {
        
        self.leftBarButtonItem=   [UIBarButtonItem itemWithTarget:self
                                                           action:@selector(touchNavLeftBtn:)
                                                            title:title];
        self.navigationItem.leftBarButtonItem =self.leftBarButtonItem;
    }
    UIButton *leftBnt=(UIButton*)self.leftBarButtonItem.customView;
    [leftBnt setTitle:title forState:UIControlStateNormal];
    [leftBnt setTitleColor:titleColor  forState:UIControlStateNormal];
}




-(void)defRightBarTitle:(NSString*)title{
    if (!self.rightBarButtonItem) {
        
        self.rightBarButtonItem=   [UIBarButtonItem itemWithTarget:self
                                                            action:@selector(touchNavRightBtn:)
                                                             title:title];
        self.navigationItem.rightBarButtonItem =self.rightBarButtonItem;
    }
    
    UIButton *rightBnt=(UIButton*)self.rightBarButtonItem.customView;
    [ rightBnt setTitle:title forState:UIControlStateNormal];
    [ rightBnt setTitleColor:kRGBA(255, 255, 255, 1)  forState:UIControlStateNormal];
    [rightBnt sizeToFit];
}

-(void)defRightBarTitle:(NSString*)title color:(UIColor *)titleColor {
    
    
    if (!self.rightBarButtonItem) {
        
        self.rightBarButtonItem=   [UIBarButtonItem itemWithTarget:self
                                                            action:@selector(touchNavRightBtn:)
                                                             title:title];
        self.navigationItem.rightBarButtonItem =self.rightBarButtonItem;
    }
    
    UIButton *rightBnt=(UIButton*)self.rightBarButtonItem.customView;
    [rightBnt setTitle:title forState:UIControlStateNormal];
    [rightBnt setTitleColor:titleColor forState:UIControlStateNormal];
    [rightBnt sizeToFit];
}

-(void)defRightBarImage:(NSString*)image{
    if (!self.rightBarButtonItem) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button addTarget:self action:@selector(touchNavRightBtn:) forControlEvents:UIControlEventTouchUpInside];
        UIImage *img = [UIImage imageNamed:image];
        [button setImage:[img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
        [button sizeToFit];
        if (button.bounds.size.width < 40) {
            CGFloat width = 40 / button.bounds.size.height * button.bounds.size.width;
            button.bounds = CGRectMake(0, 0, width, 40);
            button.imageEdgeInsets = UIEdgeInsetsMake(0,(width - img.size.width), 0,0);
        }else{
            button.imageEdgeInsets = UIEdgeInsetsZero;
        }
        self.rightBarButtonItem=   [[UIBarButtonItem alloc] initWithCustomView:button];
        self.navigationItem.rightBarButtonItem =self.rightBarButtonItem;
    }
    UIButton *rightBnt=(UIButton*)self.rightBarButtonItem.customView;
    [rightBnt setImage:[[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]forState:UIControlStateNormal];
    
}
-(void)touchNavLeftBtn:(UIButton *)sender{
    [self.view endEditing:YES];
    
}


-(void)touchNavRightBtn:(UIButton *)sender{
    
}


-(void)jj_scrollView:(UIScrollView*)scrollView
        sontentInset:(UIEdgeInsets)insets{
    if (@available(iOS 11.0, *)) {
        scrollView.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets=NO;
    }
    scrollView.contentInset = insets;
    scrollView.scrollIndicatorInsets = insets;
}



#pragma mark - 添加多个导航栏控件

//设置导航栏按钮之间的间距控件
#define kNavBarButtonItem [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]
/**
 导航栏右侧按钮设置：最多添加三个
 @param target          对象
 @param backImageArr    图片数组
 @param negativeSpace_w 间距
 @param action          方法
 */
- (void)jj_rightNaviWithImageArr:(NSArray *)backImageArr negativeSpace:(CGFloat)negativeSpace_w showOriginal:(BOOL)isShowOriginalColor action:(SEL)action{
    //右侧按钮1（更多按钮）

    UIButton *btn = [self createnNavBtnWithTag:11 showOriginalColor:isShowOriginalColor backImageStr:backImageArr.firstObject action:action];
    UIBarButtonItem *newItem_more = [[UIBarButtonItem alloc]initWithCustomView:btn];
    //间距设置
    UIBarButtonItem *negativeSpacer = kNavBarButtonItem;
    
    if (backImageArr.count == 1) {
        //设置为负值可以减小按钮之间的间距，有一定效果
        if (negativeSpace_w) {
            negativeSpacer.width = negativeSpace_w;
        }else{
            negativeSpacer.width = -10;
        }
        self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, newItem_more, nil];
        return;
    }
    
    //右侧按钮2（下拉按钮）
    UIButton *btn1 = [self createnNavBtnWithTag:21 showOriginalColor:isShowOriginalColor backImageStr:backImageArr[1] action:action];
    UIBarButtonItem *newItem_hide = [[UIBarButtonItem alloc]initWithCustomView:btn1];
    
    //设置导航栏多个按钮之间的间距
    UIBarButtonItem *negativeSpacer2 = kNavBarButtonItem;
    UIBarButtonItem *negativeSpacer3 = kNavBarButtonItem;
    if (backImageArr.count == 2) {
        //设置为负值可以减小按钮之间的间距，有一定效果
        if (negativeSpace_w) {
            negativeSpacer.width = negativeSpace_w;
            negativeSpacer2.width = negativeSpace_w;
            negativeSpacer3.width = negativeSpace_w;
        }else{
            negativeSpacer.width = -10;
            negativeSpacer2.width = -10;
            negativeSpacer3.width = -10;
        }
        //将按钮加入到两侧的导航栏中(显示顺序是：从右到左排列)
        self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,newItem_more,negativeSpacer2,newItem_hide,negativeSpacer3,nil];
        return;
    }
    
    //右侧按钮3
    UIButton *btn2 = [self createnNavBtnWithTag:31 showOriginalColor:isShowOriginalColor backImageStr:backImageArr.lastObject action:action];
    UIBarButtonItem *newItem_thir = [[UIBarButtonItem alloc]initWithCustomView:btn2];
    
    //设置导航栏多个按钮之间的间距
    UIBarButtonItem *negativeSpacer4 =kNavBarButtonItem;
    if (backImageArr.count == 3) {
        //设置为负值可以减小按钮之间的间距，有一定效果
        if (negativeSpace_w) {
            negativeSpacer.width = negativeSpace_w;
            negativeSpacer2.width = negativeSpace_w;
            negativeSpacer3.width = negativeSpace_w;
            negativeSpacer4.width = negativeSpace_w;
        }else{
            negativeSpacer.width = -10;
            negativeSpacer2.width = -10;
            negativeSpacer3.width = -10;
            negativeSpacer4.width = -10;
        }
        //将按钮加入到两侧的导航栏中(显示顺序是：从右到左排列)
        self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,newItem_more,negativeSpacer2,newItem_hide,negativeSpacer3,newItem_thir,negativeSpacer4,nil];
        return;
    }
}

/**
 导航栏左侧按钮添加设置：最多添加两个
 @param backImageArr    图片名称数组
 @param negativeSpace_w 距离左侧间距，默认-10：负值可以减小按钮之间的间距
 @param action          响应方法
 */
-(void)jj_leftNaviBtnWithImageArr:(NSArray *)backImageArr negativeSpace:(CGFloat)negativeSpace_w showOriginal:(BOOL)isShowOriginalColor action:(SEL)action{
    //左侧按钮
    UIButton *btn = [self createnNavBtnWithTag:10 showOriginalColor:isShowOriginalColor backImageStr:backImageArr.firstObject action:action];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    //设置导航栏多个按钮之间的间距
    UIBarButtonItem *negativeSpacer = kNavBarButtonItem;
    if (backImageArr.count == 1) {
        //设置为负值可以减小按钮之间的间距，有一定效果
        if (negativeSpace_w) {
            negativeSpacer.width = negativeSpace_w;
        }else{
            negativeSpacer.width = -10;
        }
        //将按钮加入到两侧的导航栏中
        self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, leftItem, nil];
        return;
    }
    
    //右侧按钮2（下拉按钮）
    UIButton *btn1 = [self createnNavBtnWithTag:20 showOriginalColor:isShowOriginalColor backImageStr:backImageArr.lastObject action:action];
    UIBarButtonItem *newItem_hide = [[UIBarButtonItem alloc]initWithCustomView:btn1];
    
    //设置导航栏多个按钮之间的间距
    UIBarButtonItem *negativeSpacer2 =kNavBarButtonItem;
    UIBarButtonItem *negativeSpacer3 = kNavBarButtonItem;
    if (backImageArr.count == 2) {
        //设置为负值可以减小按钮之间的间距，有一定效果
        if (negativeSpace_w) {
            negativeSpacer.width = -10;
            negativeSpacer2.width = negativeSpace_w;
            negativeSpacer3.width = -10;
        }else{
            negativeSpacer.width = -10;
            negativeSpacer2.width = 20;
            negativeSpacer3.width = -10;
        }
        //将按钮加入到两侧的导航栏中(显示顺序是：从右到左排列)
        self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,leftItem,negativeSpacer2,newItem_hide,negativeSpacer3,nil];
        return;
    }
    
}

- (UIButton *)createnNavBtnWithTag:(NSInteger)tag showOriginalColor:(BOOL)showOriginal backImageStr:(NSString *)backImageStr action:(SEL)action{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.tag = tag;
    UIImage *img_hide;
    if (showOriginal) {
        img_hide = kOriImage(backImageStr);
    }else{
        img_hide = [UIImage imageNamed:backImageStr];
    }
    btn.frame = CGRectMake(0, 0, img_hide.size.width, img_hide.size.height);
    [btn setImage:img_hide forState:UIControlStateNormal];
    [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

@end
